package com.kly.sc.api.application.service.user.impl;

import cn.hutool.crypto.SecureUtil;
import com.aliyun.openservices.shade.io.netty.util.internal.StringUtil;
import com.fqgj.base.services.redis.RedisClient;
import com.fqgj.common.api.Response;
import com.kly.sc.api.application.constant.ApiRedisKeys;
import com.kly.sc.api.application.controller.im.response.IMPopResponse;
import com.kly.sc.api.application.controller.im.response.TipsResponse;
import com.kly.sc.api.application.helper.RequestLocalInfo;
import com.kly.sc.api.application.service.user.IMNoticeService;
import com.kly.sc.api.application.utils.Result;
import com.kly.sc.api.application.utils.RpcUtils;
import com.kly.sc.api.infras.utils.StringUtils;
import com.kly.user.constant.RedisKey;
import com.kly.user.dto.ImNoticeConfigDto;
import com.kly.user.dto.UserBaseDto;
import com.kly.user.service.UserMsgService;
import com.kly.user.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

import static com.kly.sc.api.application.controller.im.IMMessageController.EFFECTIVE_IN_5_MIN;

@Service
public class IMNoticeServiceImpl implements IMNoticeService {

    @Resource
    UserMsgService userMsgService;

    @Resource
    UserService userService;

    @Resource
    RedisClient redisClient;

    @Override
    public Result<IMPopResponse> getIMPop(Integer userMessageNum) {
        final String appCode = RequestLocalInfo.getRequestBasicInfo().getAppCode();
        String userCode = RequestLocalInfo.getCurrentUser().getUserCode();
        UserBaseDto userBaseDto = RpcUtils.getResult(userService.getUserBaseInfo(userCode));
        if (userBaseDto != null && (StringUtils.isEmpty(userBaseDto.getChannel()) || userBaseDto.getChannel().equals("Organic"))) {
            return Result.ok();
        }

        ImNoticeConfigDto imNoticeConfigDto = userMsgService.getNoticeConfig(userMessageNum, appCode);
        if (imNoticeConfigDto != null) {
            IMPopResponse imPopResponse = new IMPopResponse();
            imPopResponse.setPopTitle(imNoticeConfigDto.getPopTitle());
            imPopResponse.setPopContent(imNoticeConfigDto.getPopContent());
            imPopResponse.setPopPicUrl(imNoticeConfigDto.getPopPicUrl());
            imPopResponse.setPopButton(imNoticeConfigDto.getPopButton());
            return Result.ok(imPopResponse);
        }
        return Result.ok();
    }

    @Override
    public Result<TipsResponse> getTips(Integer conversationType) {
        String userCode = RequestLocalInfo.getCurrentUser().getUserCode();
        final String appCode = RequestLocalInfo.getRequestBasicInfo().getAppCode();
        UserBaseDto userBaseDto = RpcUtils.getResult(userService.getUserBaseInfo(userCode));
        if (userBaseDto != null && (StringUtils.isEmpty(userBaseDto.getChannel()) || userBaseDto.getChannel().equals("Organic"))) {
            return Result.ok();
        }

        Object object = redisClient.get(RedisKey.MSG_TIPS, userCode, conversationType.toString());
        if (object != null) {
            return Result.ok();
        }

        TipsResponse tipsResponse = new TipsResponse();
        tipsResponse.setConversationType(conversationType);
        List<String> messageList = RpcUtils.getResult(userMsgService.getGreetingContent("127", 3, appCode));
        if (!messageList.isEmpty()) {
            tipsResponse.setTitle(getTipsTitle(conversationType));
            tipsResponse.setContents(messageList);
        }


        LocalDateTime now = LocalDateTime.now();
        // 设置时间为当天的最后时刻（23:59:59）
        LocalTime endOfDay = LocalTime.of(23, 59, 59);
        // 计算当前时间到当天最后时刻的时间差
        long remainingSeconds = now.until(LocalDateTime.of(now.toLocalDate(), endOfDay), ChronoUnit.SECONDS);
        redisClient.set(RedisKey.MSG_TIPS, userCode, (24 * 60 * 60) + remainingSeconds, userCode, conversationType.toString());

        return Result.ok(tipsResponse);
    }

    /**
     * 读取TipsTitle
     *
     * @param conversationType
     * @return
     */
    private String getTipsTitle(Integer conversationType) {
        String tipsTitle = "";
        if (conversationType == 1) {
            tipsTitle = "Don't be shy";
        } else if (conversationType == 2) {
            tipsTitle = "Be a little more courageous";
        } else {
            tipsTitle = "Go flirt boldly";
        }
        return tipsTitle;
    }

}
